# encoding=utf-8

import time
import xmlrpclib

from flask import Flask, render_template
from flask import request, redirect, Blueprint

from cloud_testing.utils import register_ip, query_ip, save_report, query_report


# 组件加密检测
encrypt = Blueprint('encrypt', __name__,
                       url_prefix='/encrypt',
                       template_folder='templates',
                       static_folder='static')

 
@encrypt.route('/route/<name>', methods=['GET', 'POST'])
def memory_route(name):
    if name == 'openstack':
        return openstack_init()
    elif name == 'spice':
        return spice_init()
    elif name == 'vnc':
        return vnc_init()
    else:
        return render_template('content.html',data={},welcomePage=False)
    
def openstack_init():
    introduce = {
        'content':u'写上自己需要的内容',
        'method':u'测试方法',
        'setup':u'设置：centos7，wireshark...',
        'testmethod':u'测试方式,点击按钮即可',
        'report':u'报告',
        'picture':'/static/img/openstack.jpg'
        }
    return render_template('content.html', introduce=introduce, welcomePage=False, nav_a=u'数据加密', nav_b=u'openstack组件加密检测')

def spice_init():
    introduce = {
        'content':u'写上自己需要的内容',
        'method':u'测试方法',
        'setup':u'设置：centos7，wireshark...',
        'testmethod':u'测试方式,点击按钮即可',
        'report':u'报告',
        'picture':'/static/img/openstack.jpg'
        }
    return render_template('content.html', introduce=introduce, welcomePage=False, nav_a=u'数据加密', nav_b=u'spice')

def vnc_init():
    introduce = {
        'content':u'写上自己需要的内容',
        'method':u'测试方法',
        'setup':u'设置：centos7，wireshark...',
        'testmethod':u'测试方式,点击按钮即可',
        'report':u'报告',
        'picture':'/static/img/openstack.jpg'
        }
    return render_template('content.html', introduce=introduce, welcomePage=False, nav_a=u'数据加密', nav_b=u'vnc')

'''    
@encry.route('/test', methods=['POST', 'GET'])
def test():
    if request.method == 'POST':
        ip = request.form['ip']
        port = request.form['port']
        count = 30
        # print ip,port     /report
        host = "http://" + ip + ':' + port
        proxy = xmlrpclib.ServerProxy(host)
        proxy.processPack(str(count))
        time.sleep(count)
        result = proxy.stringMatch()
        # print (result.data)#存入report表
        save_report({"ip":ip, "port":port, "server_name":"zjjc", "result":(result.data)})
        return 'success'
    else:
        return 'error'
@encry.route('/report', methods=['POST', 'GET']) 
def report():
    if request.method == 'POST':
        ip = request.form['ip']
        port = request.form['port']
        return str(query_report(ip, port, 'zjjc'))
    return 'error'
              
@encry.route('/<nav1>/<nav2>')
def encryption(nav1, nav2):
    introduce= {
        'content':u'写上自己需要的内容',
        'method':u'测试方法',
        'setup':u'设置：centos7，wireshark...',
        'testmethod':u'测试方式,点击按钮即可',
        'report':u'报告',
        'picture':'/static/img/openstack.jpg'
        }
    setup = u"样例"
    testmethod = u"样例"
    report = u"样例"
    picture = "/static/img/openstack.jpg"
    
    return render_template('content.html', introduce=introduce,content=u'vdsm作为ovirt平台的主机代理，它为 oVirtEngine 的主机，虚拟机，网络和存储管理提供了所有必需的功能。     采用XMLRPC调用，在主机端作为服务进程监听54321端口，与engine进行通信。', method=u'本测试指标的测试对象是VDSM，结果是获取VDSM通信流。<br>测试包括三个步骤，配置、测试方法、报告。', list=query_ip(), setup=setup, report=report, testmethod=testmethod, picture=picture, nav_a=nav1, nav_b=nav2)

@encry.route('/kvm')
def kvm():
    content = get()
    return render_template('content.html', content='kvm...............')

def get():
    return {'name':'value'}

'''


    
  
